|
1
|
|
|
import test from 'ava'; |
|
2
|
|
|
import sinon from 'sinon'; |
|
3
|
|
|
import fetchMock from 'fetch-mock'; |
|
4
|
|
|
import callAPIMethod from '../src/callAPIMethod'; |
|
5
|
|
|
import APIError from '../src/error'; |
|
6
|
|
|
|
|
7
|
|
|
const matcher = '*'; |
|
8
|
|
|
|
|
9
|
|
|
const ResponseGood = new Response( |
|
10
|
|
|
JSON.stringify({ sample: 'data'}), |
|
11
|
|
|
{ |
|
12
|
|
|
status: 200 |
|
13
|
|
|
} |
|
14
|
|
|
); |
|
15
|
|
|
|
|
16
|
|
|
const ResponseBad = new Response(null, { |
|
17
|
|
|
status: 500 |
|
18
|
|
|
}); |
|
19
|
|
|
|
|
20
|
|
|
test.serial('default params', async t => { |
|
21
|
|
|
fetchMock.get(matcher, {}); |
|
22
|
|
|
await callAPIMethod(); |
|
23
|
|
|
|
|
24
|
|
|
t.is(fetchMock.lastUrl(matcher), '/api', 'correct url'); |
|
25
|
|
|
t.deepEqual(fetchMock.lastOptions(matcher), { |
|
26
|
|
|
cache: 'default', |
|
27
|
|
|
credentials: 'include', |
|
28
|
|
|
method: 'GET', |
|
29
|
|
|
mode: 'cors' |
|
30
|
|
|
}, 'correct options'); |
|
31
|
|
|
|
|
32
|
|
|
fetchMock.restore(); |
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
|
|
test.serial('200 reponse', async t => { |
|
36
|
|
|
fetchMock.get(matcher, ResponseGood); |
|
37
|
|
|
const result = await callAPIMethod(); |
|
38
|
|
|
|
|
39
|
|
|
t.deepEqual(result, { sample: 'data' }, 'correct response body'); |
|
40
|
|
|
|
|
41
|
|
|
fetchMock.restore(); |
|
42
|
|
|
}); |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
test.serial('500 reponse', async t => { |
|
46
|
|
|
fetchMock.get(matcher, ResponseBad); |
|
47
|
|
|
|
|
48
|
|
|
const result = await callAPIMethod(); |
|
49
|
|
|
|
|
50
|
|
|
t.true(result instanceof Error, 'instance of Error'); |
|
51
|
|
|
t.true(result instanceof APIError, 'instance of APIError'); |
|
52
|
|
|
|
|
53
|
|
|
fetchMock.restore(); |
|
54
|
|
|
}); |
|
55
|
|
|
|
|
56
|
|
|
test.serial('fetch options', async t => { |
|
57
|
|
|
fetchMock.post(matcher, ResponseGood); |
|
58
|
|
|
|
|
59
|
|
|
const APINamespace = 'rest-api'; |
|
60
|
|
|
const namespace = 'user'; |
|
61
|
|
|
const fetchOptions = { method: 'POST'}; |
|
62
|
|
|
const methodOptions = { |
|
63
|
|
|
path: 'path', |
|
64
|
|
|
query: { edit: true }, |
|
65
|
|
|
options: { |
|
66
|
|
|
credentials: 'omit' |
|
67
|
|
|
}, |
|
68
|
|
|
method: 'text' |
|
69
|
|
|
}; |
|
70
|
|
|
const spyResponseGood = sinon.spy(ResponseGood, 'text'); |
|
71
|
|
|
|
|
72
|
|
|
fetchMock.post(matcher, {}); |
|
73
|
|
|
|
|
74
|
|
|
await callAPIMethod(APINamespace, fetchOptions, namespace, methodOptions); |
|
75
|
|
|
|
|
76
|
|
|
t.is(fetchMock.lastUrl(matcher), '/rest-api/user/path?edit=true', 'correct url'); |
|
77
|
|
|
t.deepEqual(fetchMock.lastOptions(matcher), { |
|
78
|
|
|
cache: 'default', |
|
79
|
|
|
credentials: 'omit', |
|
80
|
|
|
method: 'POST', |
|
81
|
|
|
mode: 'cors' |
|
82
|
|
|
}), 'correct options'; |
|
83
|
|
|
t.true(spyResponseGood.calledOnce); |
|
84
|
|
|
|
|
85
|
|
|
spyResponseGood.restore(); |
|
86
|
|
|
fetchMock.restore(); |
|
87
|
|
|
}); |
|
88
|
|
|
|
|
89
|
|
|
test.serial('unsupported Response method', async t => { |
|
90
|
|
|
fetchMock.get(matcher, ResponseGood); |
|
91
|
|
|
|
|
92
|
|
|
const APINamespace = 'rest-api'; |
|
93
|
|
|
const namespace = 'user'; |
|
94
|
|
|
const fetchOptions = { method: 'POST'}; |
|
95
|
|
|
const methodOptions = { |
|
96
|
|
|
path: 'path', |
|
97
|
|
|
options: { |
|
98
|
|
|
credentials: 'omit' |
|
99
|
|
|
}, |
|
100
|
|
|
method: 'undefined' |
|
101
|
|
|
}; |
|
102
|
|
|
|
|
103
|
|
|
const result = await callAPIMethod(APINamespace, namespace, fetchOptions, methodOptions); |
|
104
|
|
|
|
|
105
|
|
|
t.true(result instanceof Error); |
|
106
|
|
|
t.true(result instanceof TypeError); |
|
107
|
|
|
t.false(result instanceof APIError); |
|
108
|
|
|
|
|
109
|
|
|
fetchMock.restore(); |
|
110
|
|
|
}); |